package com.dao.impl;

import java.util.List;

import org.springframework.stereotype.Repository;

import persist.article.Article;
import persist.article.ArticleComment;
import persist.article.ArticleType;

import com.dao.ArticleDao;
import com.myHibernateDao.MyHibernateDaoSupport;

@Repository("articleDao")
@SuppressWarnings("unchecked")
public class ArticleDaoImpl extends MyHibernateDaoSupport implements ArticleDao {

    public Article getArticle(Integer id) {
        return (Article) super.getHibernateTemplate().get(Article.class, id);
    }

    public ArticleType getArticleTypeByName(String name) {
        String sql = "from ArticleType at where at.typeName=?";
        List<ArticleType> list = super.getHibernateTemplate().find(sql, name);
        if (list.size() >= 1) {
            return (ArticleType) list.get(0);
        } else
            return null;
    }

    public ArticleComment getArticleComment(Integer id) {
        return (ArticleComment) super.getHibernateTemplate().get(ArticleComment.class, id);
    }

    public ArticleType getArticleType(Integer id) {
        return (ArticleType) super.getHibernateTemplate().get(ArticleType.class, id);
    }

    public void save(Object obj) {
        super.getHibernateTemplate().save(obj);
    }

    public void update(Object obj) {
        super.getHibernateTemplate().update(obj);
    }

    public void deleteArticleById(Integer id) {
        super.getHibernateTemplate().delete(this.getArticle(id));
    }

    public void deleteArticleComment(Integer id) {
        super.getHibernateTemplate().delete(this.getArticleComment(id));
    }

    public void deleteArticleType(Integer id) {
        super.getHibernateTemplate().delete(this.getArticleType(id));
    }

    public void deleteByObject(Object obj) {
        super.getHibernateTemplate().delete(obj);
    }

    public List<ArticleType> findAllType() {
        return super.getHibernateTemplate().find("from ArticleType");
    }

    public List<Article> findArticleByTypeId(Integer type_id) {
        return super.getHibernateTemplate().find("from Article a where a.articleType.id=? order by time desc", type_id);
    }

    public Article findArticleByTypeIdLimit(Integer type_id) {
        List<Article> list = super.findByPage("from Article a where a.articleType.id=? order by time desc", type_id, 0 ,1);
        return list.size() > 0 ? list.get(0) : null;
    }

    public List<Article> findAllArticle() {
        return super.getHibernateTemplate().find("from Article");
    }

    public Integer countArticles() {
        return ((Long) super.getHibernateTemplate().iterate("select count(id) from Article").next()).intValue();
    }

    public Article findArticleById(int id) {
        List<Article> a = super.getHibernateTemplate().find("from Article where id=" + id);
        return a.size() == 1 ? a.get(0) : new Article();
    }

    public List<ArticleComment> findCommentByArticleId(int id) {
        String hql = "from ArticleComment ac where ac.article.id=" + id + " order by ac.time desc";
        return super.getHibernateTemplate().find(hql);
    }

    public List<Article> findArticleByPage(int pageNo, int pageSize, int type) {
        int offset = (pageNo - 1) * pageSize;
        String hql;
        if (type == 0)
            hql = "from Article order by time desc";
        else
            hql = "from Article a where a.articleType.id=" + type + " order by time desc";
        return super.findByPage(hql, offset, pageSize);
    }

    public List<ArticleComment> findLatestComment(int top) {
        return super.findByTop("from ArticleComment order by time desc", top);
    }

    public List<Article> findLatestArticle(int top) {
        return super.findByTop("from Article order by time desc", top);
    }

    public List<Article> findInArticle(String keyword) {
        String hql = "from Article where title like ? or author like ? or content like ? order by time desc";
        keyword = "%" + keyword + "%";
        Object[] obj = { keyword, keyword, keyword };
        return super.getHibernateTemplate().find(hql, obj);
    }

}
